package main

import "fmt"

func f() {
	defer func() {
		if r := recover(); r != nil { //第三步
			fmt.Println("Recovered in f", r)
		}
	}()
	fmt.Println("Calling g.")
	g(0)
	fmt.Println("Returned normally from g.") //第四步
}

func g(i int) {
	if i > 3 {
		fmt.Println("Panicking start!")
		//第一步:函数g调用panic,g的执行就会停止，g的defer函数调用会被执行.
		panic(fmt.Sprintf("%v", i))
		fmt.Println("Panicking end!")
	}
	defer fmt.Println("Defer in g", i) //第二步
	fmt.Println("Printing in g", i)
	g(i + 1)
}

func main() {
	f()
	fmt.Println("Returned normally from f.")
}
